package main

import "fmt"

func printSlice(s [] int){
	fmt.Printf("%v, len=%d, cap=%d\n", s, len(s), cap(s))
}

func main() {
	fmt.Println("Creating slice")
	//使用声明创建slice
	var s []int  //zero value for slice is nil
	for i:=0; i<100; i++{
		printSlice(s)
		s = append(s, 2*i+1)
	}
	fmt.Println(s)

	//其他创建slice的方法
	s1 := []int{2,4,6,8}
	printSlice(s1)

	s2 := make([]int, 16)
	printSlice(s2)

	//第二个参数是len, 第三个参数是cap
	s3 := make([]int, 10, 32)
	//fmt.Printf("s3=%v, len=%d, cap=%d\n", s3, len(s3), cap(s3))
	printSlice(s3)

	fmt.Println("Coping slice")
	copy(s2, s1)
	printSlice(s2)

	fmt.Println("Deleting elements for slice")
	//通过跳过索引，拼接的方式删除
	s2 = append(s2[:3], s2[4:]...)
	printSlice(s2)

	fmt.Println("Poping from front")
	front := s2[0]
	s2 = s2[1:]

	fmt.Println(front)
	printSlice(s2)

	fmt.Println("Poping from tail")
	tail := s2[len(s2) -1]
	s2 = s2[:len(s2)-1]

	fmt.Println(tail)
	printSlice(s2)


}
